﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ODS
{
    public partial class TotalMessagePage : PhoneApplicationPage
    {
        public bool IsShowingPopUp = false;

        public TotalMessagePage()
        {
            InitializeComponent();

            MessageNotificationView.ParentView = this;
            this.DataContext = App.MainViewModel.TotalMessageViewModel;
            //pop up
            sb_fadeOut_popUp.Completed += sb_fadeOut_popUp_Completed;
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            showWaiting();

            await App.MainViewModel.LoadAllMessagesOfUser();

            hiddenWaiting();
            
        }

        void showWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Visible;
        }

        void hiddenWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Collapsed;
        }

        void sb_fadeOut_popUp_Completed(object sender, EventArgs e)
        {
            popupMessage.IsOpen = false;
        }

        public void showPopUpWithAnimation(String message)
        {
            tbl_messagePopUp.Text = message;
            IsShowingPopUp = true;
            RootPopupBorder.Opacity = 0;
            popupMessage.IsOpen = true;
            sb_fadeIn_popUp.Begin();
        }

        public void hiddenPopUpWithAnimation()
        {
            RootPopupBorder.Opacity = 1;
            IsShowingPopUp = false;
            sb_fadeOut_popUp.Begin();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (IsShowingPopUp)
            {
                hiddenPopUpWithAnimation();
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void btn_back_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        bool isTapOnRootPopupBorder = false;
        private void grid_rootLayoutPopup_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!isTapOnRootPopupBorder)
            {
                hiddenPopUpWithAnimation();
            }
            isTapOnRootPopupBorder = false;
        }

        private void RootPopupBorder_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            isTapOnRootPopupBorder = true;
        }
    }
}